checkLogin();

var ref = parseInt(GetQueryString('ref'));

/*var login = getCache(login_key);
var sid = login.sid;*/
var shop_center ,range_height= null;
init_shop();

//获取水店的
function init_shop() {
    //获取水票须知
    var success = function (res) {
        var info = res.data.info;
        console.log(info)
        shop_center = info.shop_position;
        if(!isEmpty(shop_center)){
            lat = shop_center.split(',')[0];
            lng = shop_center.split(',')[1];
            shop_center = new qq.maps.LatLng(lat,lng);
        }
        range_height = info.send_area_radius * 1000;
        init();
    }

    getData(shop_info,comParams(),success);
}

var time = 0;//表示首次绑定账户资料

function init() {
    //获取账户资料信息
    var success = function (res) {

        var info = res.data.info;
        //如果账户资料相关为空
        if (isEmpty(info.name) && isEmpty(info.mobile) && isEmpty(info.province_id) && isEmpty(info.city_id) && isEmpty(info.address)) {
            $('input[name="mobile"]').removeAttr('readonly');
            change_mobile = true;
            $('.change_but').hide();
            $('.mobile').val('');
            $('.data3').show();
        }else {
            time =1;
        }
        //将数据放入对应位置
        $('.name').val(info.name);
        $('.mobile').val(info.mobile);
        $('.company_name').val(info.company_name);
        $('.address').val(info.address);
        $('#old_mobile').val(info.mobile);
        if (info.mobile != undefined && info.mobile != null && info.mobile != '') {
            $('input[name="mobile"]').attr('readonly', 'readonly');
        }

        //拼接地址
        var area_info = '';
        if (info.province != undefined && info.province != null && info.province != '') {
            area_info = info.province + ' ';
        }
        if (info.city != undefined && info.city != null && info.city != '') {
            area_info += info.city + ' ';
        }
        if (info.area != undefined && info.area != null && info.area != '') {
            area_info += info.area;
        }
        $('.area_info').val(area_info);
        $('.radio1').prop('checked', false);
        //判断是家庭还是单位
        if (info.type == 1) {
            $('.company').prop('checked', 'checked').attr('checked',true);
            $('.data5').show();
        } else {
            $('.home').prop('checked', 'checked').attr('checked',true);
            $('.data5').hide();
        }

        var choosed_type = info.type;//单位还是家庭 1：单位

        //点击选择 单位还是家庭
        $('.radio1').on('tap', function () {
            var data = parseInt(this.getAttribute('data'));
            if (data == 10) {
                $('.data5').show();
                choosed_type = 1;
            } else {
                $('.data5').hide();
            }
        });
        $('.mui-radio').on('tap', function () {
            var data = parseInt($(this).find('input').attr('data'));
            if (data == 10) {
                $('.data5').show();
                choosed_type = 1;
            } else {
                $('.data5').hide();
            }
        });

        //将默认的地址信息放入隐藏域
        $("#province_id").val(info.province_id);
        $("#city_id").val(info.city_id);
        $("#area_id").val(info.area_id);
        var change_mobile = false;
        //点击修改手机号

        $('.change_but').on('tap', function () {
            $('input[name="mobile"]').removeAttr('readonly');
            change_mobile = true;
            $('.change_but').hide();
            $('.mobile').val('');
            $('.data3').show();

        })

        //点击获取验证码
        /*$('.get_code').on('tap', function () {*/
        $('.get_code').bind('click',function () {

            $(this).addClass('mui-disabled').attr('disabled', true);
            var mobile = $('.mobile').val();
            if (mobile == undefined || mobile == null || mobile == '') {
                malert('请填写联系方式');
                $('.get_code').removeClass('mui-disabled').removeAttr('disabled');
                return;
            }
            var success = function (res) {
                console.log(res)
                var info = res.sms_time;

                $('.get_code').text(info + 's');
                var intervarId = null;

                function count_down() {
                    info--;
                    if (info <= 0) {
                        clearInterval(intervarId);
                        $('.get_code').removeClass('mui-disabled').removeAttr('disabled');
                        $('.get_code').text('请重新获取');
                        return;
                    }
                    $('.get_code').text(info + 's');
                }

                intervarId = setInterval(function () {
                    count_down();
                }, 1000);
            }
            getData(member_sms_captcha, combineParams({mobile: mobile}), success);
        })

        //判断单位时，填写单位地址
        var is_company = function () {
            var status = true;
            var company = $('.company_name').val();
            if (choosed_type != 1 && (company == undefined || company == null || company == '')) {
                status = false;
            }
            return {
                status: status, // or false
                message: '请填写单位地址'
            };
        }

        //修改手机时，需要填写验证码
        var is_card = function () {
            var status = true;
            var card = $('.captcha').val();
            if (change_mobile && (card == undefined || card == null || card == '')) {
                status = false;
            }
            return {
                status: status, // or false
                message: '请填写验证码'
            };
        }

        //获取原始窗口的高度
        var originalHeight=document.documentElement.clientHeight || document.body.clientHeight;
        window.onresize=function(){
            //软键盘弹起与隐藏  都会引起窗口的高度发生变化
            var  resizeHeight=document.documentElement.clientHeight || document.body.clientHeight;
            if(resizeHeight*1<originalHeight*1){ //resizeHeight<originalHeight证明窗口被挤压了
                $('.sure-submit').css('display','none');
                /*$(".mui-iframe-wrapper").css('bottom','0px');*/
            }else{
                $('.sure-submit').css('display','block');
                /*$(".mui-iframe-wrapper").css('bottom','50px');*/
            }
        }
    }
    getData(member_info, comParams(), success);
}

//点选输入框时 颜色变化
$('.edit-container').on('tap', function () {
    var type = parseInt(this.getAttribute('data'));
    if (type == undefined || type == null || type == '') {
        return;
    }
    $('.edit-container').removeClass('border_color');
    $('.data' + type).addClass('border_color');

})

var _getParam = function (obj, param) {
    return obj[param] || '';
};
var cityPicker3 = new mui.PopPicker({
    layer: 3
});

cityPicker3.setData(cityData3);
cityPicker3.pickers[0].setSelectedIndex(0);
cityPicker3.pickers[0].setSelectedValue('110000');
cityPicker3.pickers[1].setSelectedIndex(0);
cityPicker3.pickers[1].setSelectedValue('110000');
cityPicker3.pickers[2].setSelectedIndex(0);
cityPicker3.pickers[2].setSelectedValue('110101');
var showCityPickerButton = document.getElementById('showCityPicker3');

//监听重选地址
showCityPickerButton.addEventListener('tap', function (event) {
    cityPicker3.show(function (items) {
        showCityPickerButton.value = _getParam(items[0], 'text') + " " + _getParam(items[1], 'text') + " " + _getParam(items[2], 'text');
        $("#province_id").val(_getParam(items[0], 'value'));
        $("#city_id").val(_getParam(items[1], 'value'));
        $("#area_id").val(_getParam(items[2], 'value'));
        //返回 false 可以阻止选择框的关闭
        //return false;
        //判断详细地址是否填写
        var change_address = $('#address').val();
        if(!isEmpty(change_address) && time == 0){
            var area_info = $('.area_info').val();
            //获取经纬度
            getLatLng(area_info+change_address,area_info);
        }

    });
}, false);

//监听详细地址变化
address.addEventListener("change", function(event) {
    //判断地区有没有选择
    var area_info = $('.area_info').val();
    if(!isEmpty(area_info) && time == 0){
        var change_address = $('#address').val();
        //获取经纬度
        getLatLng(area_info+change_address,area_info);
    }
});
var geocoder ,lat ,lng ,mubiao ,area_position= null;
function getLatLng(info,area_info) {

    if(isEmpty(info)){
        return;
    }
    //根据已经有的地址获取经纬度
    //地址和经纬度之间进行转换服务
    geocoder = new qq.maps.Geocoder({
        //设置服务请求成功的回调函数
        complete: function (result) {
            console.log(result.detail.location)
            lat = result.detail.location.lat;
            lng = result.detail.location.lng;
            area_position = lat + ',' + lng;
            //判断是否在配送范围
            //弹出所输入坐标后对比中心坐标 看是否在配送范围
            mubiao = result.detail.location
            if(isEmpty(shop_center) || isEmpty(range_height) || range_height <=0){
                return;
            }
            var length = qq.maps.geometry.spherical.computeDistanceBetween(shop_center, mubiao);
            console.log(length)
            if(length <= range_height){ //表示在配送范围内
                area_info = area_info.replace(/\s+/g,"");
                $('#add_address').val(1);
                $('#area_position').val(area_position);
                $('#position_address').val(area_info);
            }else {
                $('#add_address').val(0);
                $('#area_position').val(0);
                $('#position_address').val('');
            }

        },
        //若服务请求失败，则运行以下函数
        error: function () {
            mui.toast('定位失败，重新填写');
        }
    })

    //如果没有经纬度 那么 按照店铺地址获取经纬度
    geocoder.getLocation(info);

}

var is_mobile = function(e) {
    var status = true;
    if (!(/^1\d{10}$/.test( $(e).val()))){
        status = false;
    }
    return {
        status: status, // or false
        message: '请填写正确的手机号'
    };
}




var ajaxOption = {
    url: member_add_information,
    method: 'POST',
    data: comParams(),
    success: function (res) {
        //保存成功  将修改的电话追加到 缓存中
        var mobile = $('.mobile').val();
        var login_text = getCache(login_key);
        if(!isEmpty(login_text) && isEmpty(login_text.mobile)){
            login_text.mobile = mobile;
            console.log(login_text)
            setCache(login_key, login_text);
        }

        setTimeout(function(){
            if (ref) {
                // 1.零售订水  2.水票订水
                if (ref == 1) {
                    locationReplace('pay-order.html');
                }
                if (ref == 2) {
                    locationReplace('member-ticket.html');
                }
            } else {
                redirect('member.html');
            }
        },2000);
    }
}

checkform($('#myform'), ajaxOption);

